import 'package:get/get.dart';

import '../../../../common/api/x_ali_face_detection_assemble_control.dart';
import '../../../../common/utils/loading.dart';
import '../../../../common/utils/o2_api_manager.dart';
import '../index.dart';
import 'index.dart';

class FaceRegistrationController extends GetxController {
  FaceRegistrationController();

  final state = FaceRegistrationState();

 Map<String, String> headers = {};
        
  /// 在 onInit() 之后调用 1 帧。这是进入的理想场所
  @override
  void onReady() {
    headers[O2ApiManager.instance.tokenName] = O2ApiManager.instance.o2User?.token ?? '';
    checkMyFaceList();
    super.onReady();
  }

  /// 在 [onDelete] 方法之前调用。
  @override
  void onClose() {
    super.onClose();
  }

  String faceUrl(String id) {
    return AppAliFaceDetectionAssembleControlService.to.faceImageUrl(id);
  }

  Future<void> checkMyFaceList() async {
    Loading.show();
    state.faceList.clear();
    final list =
        await AppAliFaceDetectionAssembleControlService.to.myFaceList();
    if (list != null && list.isNotEmpty) {
      state.faceList.addAll(list);
    }
    Loading.dismiss();
  }

  Future<void> captureAndUploadPhoto() async {
    await FaceCheckPagePage.startRegistration();
    checkMyFaceList();
  }
}
